package com.hspedu.set_;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/2/7
 * @time 22:58
 */
public class HashSetSource01 {
    public static void main(String[] args) {
        Set set = new HashSet();
        Dog d1 = new Dog("jack");
        Dog d2 = new Dog("jack");
        set.add(d1);
        set.add(d2);
        System.out.println(set);
    }
}

class Dog{
    public String name;

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                '}';
    }

    public Dog(String name) {
        this.name = name;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }

    @Override
    public boolean equals(Object o){
        if (this == o ){
            return true;
        }

        if ( o instanceof Dog){
            Dog d = (Dog) o;
            if (d.name.equals(this.name)){
                return true;
            }
        }

        return false;
    }
}
